using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MathExten;
using TMPro;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Random = UnityEngine.Random;


public class PotManager : MonoBehaviour
{
    //Player would try to keep the pot at 50-70 degrees centigrate
    //This would be effected by the distance between the pot and the fire
    //keeping the pot at relevant distance of 0.5-0.7 keeps the temperature
    //0-0.5 incresces the temperature,0.7 - 1 decreases the temperature
    private float potTemperature;
    private float potLife = 90;
    
    //Curve from 1 to -1
    [SerializeField] private AnimationCurve distanceCurve;

    private Vector2 potPosition;

    private float potPos;

    private TextMeshProUGUI TMP;

    private Slider tempSlider;
    private Slider potHealth;

    private bool isChanging = false, methodType;

    private int methodTicker, methodTime = 180;
    // Start is called before the first frame update
    void Start()
    {
        potTemperature = 60;
        TMP = GameObject.Find("PotTemperature").GetComponent<TextMeshProUGUI>();
        Application.targetFrameRate = 60;
        tempSlider = GameObject.Find("Slider").GetComponent<Slider>();
        potHealth = GameObject.Find("HealthBar").GetComponent<Slider>();
    }

    // Update is called once per frame
    void Update()
    {
        ControlPotPos();
        ManagePotTemperature();
        //potTemperature -= Random.Range(-1f, 1.5f) > 0 ? -0.1f : 0.1f;
        manageSlider();
        LerpChange();
    }

    private void FixedUpdate()
    {
        //throw new NotImplementedException();
    }

    private void ManagePotTemperature()
    {
        ManagePotHealth();
        if (potLife<0)
        {
            //Dies if ticks out
            SceneManager.LoadScene("SampleScene");
        }

        potPosition = this.transform.position;
        //The pot has a relative height of -4.5f to -1.5f
        potPos = potPosition.y;
        EditPotTemperature(potPos);
        TMP.text = potTemperature.ToString();
    }

    void EditPotTemperature(float pos)
    {
        potPos = potPos.Map(-4.5f, -1.5f, 0f, 1f);
        potTemperature += distanceCurve.Evaluate(potPos);
    }

    void ControlPotPos()
    {
        float y = Input.GetAxis("Vertical");
        this.transform.position = new Vector3(this.transform.position.x,y.Map(-1f,1f,-4.5f,-1.5f) , 0f);
    }

    void manageSlider()
    {
        float sliderTemp = potTemperature.Map(0f, 100f, 0f, 1f);
        tempSlider.value = sliderTemp;
    }

    void LerpChange()
    {
        if (!isChanging)
        {
            if (methodTicker < 0)
            {
                //Cooldown time for the LerpChange Method
                methodTicker += 1;
            }
            if (methodTicker == 0)
            {
                isChanging = true;
            }
        }
        else if (isChanging)
        {
            //Choose a change method
            if (methodTicker == 0)
            {
                methodType = Random.Range(0f, 1f) > 0.5f ? true : false;
            }
            if (methodType && methodTicker >= 0)
            {
                methodTicker += 1;
                if (methodTicker < methodTime)
                {
                    potTemperature -= 0.2f;
                }
                else
                {
                    methodTicker = -60;
                    isChanging = false;
                }
            }
            else if (!methodType&& methodTicker >= 0)
            {
                methodTicker += 1;
                if (methodTicker < methodTime)
                {
                    potTemperature += 0.2f;
                }
                else
                {
                    methodTicker = -60;
                    isChanging = false;
                }
            }
        }
    }

    void ManagePotHealth()
    {
        if (potTemperature > 50f && potTemperature < 70f)
        {
            //Pot is safe
            potLife = 90;
        }
        else
        {
            //Pot will die if it's unsafe for 1.5 seconds
            potLife -= 1;
        }
        potHealth.value = potLife.Map(0f, 90f, 0f, 1f);
    }
}
